package org.cuit.lmq.handler;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.cuit.lmq.domain.common.Result;
import org.cuit.lmq.utils.WebUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Seon
 * @date 2023/3/4
 */
@Slf4j
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        Result<Object> unauthorizedError = Result.fail("用户认证失败奥", HttpStatus.HTTP_UNAUTHORIZED);
        String json = JSON.toJSONString(unauthorizedError);
        WebUtil.renderString(response, json);
    }
}
